﻿using Demo.DAL.Abstracts;
using Demo.DAL.Abstracts.User.Entities;
using Demo.Service.Abstracts.User;
using Demo.Service.Abstracts.User.Dtos;
using Microsoft.EntityFrameworkCore;
using NET.AutoWebApi.ModelBinding;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demo.Service.User
{
    public class UserService : IUserService
    {
        private readonly DemoDbContext _dbContext;
        public UserService(DemoDbContext dbContext) => _dbContext = dbContext;
        public async Task<UserGetListOutput> CreateAsync(UserCreateUpdateInput input)
        {
            var insertUserEntity = new UserEntity
            {
                Password = input.Password,
                UserName = input.UserName,
            };
            _dbContext.Users.Add(insertUserEntity);
            await _dbContext.SaveChangesAsync();

            return new UserGetListOutput
            {
                Id = insertUserEntity.Id,
                Password = insertUserEntity.Password,
                UserName = insertUserEntity.UserName,
            };
        }

        public async Task<bool> DeleteAsync(int id)
        {
            var userEntity = await _dbContext.Users.FirstOrDefaultAsync(x => x.Id == id);
            if (userEntity == null)
            {
                return true;
            }
            _dbContext.Users.Remove(userEntity);
            await _dbContext.SaveChangesAsync();
            return true;
        }

        public async Task<List<UserGetListOutput>> GetListAsync(UserGetListInput input)
        {
            var query = _dbContext.Users.AsNoTracking();
            if (input.Keyword.IsNullOrEmpty() == false)
            {
                query = query.Where(e => e.UserName.Contains(input.Keyword));
            }
            var list = await query.Select(e => new UserGetListOutput
            {
                Id = e.Id,
                UserName = e.UserName,
                Password = e.Password,
            }).ToListAsync();

            return list;
        }

        public async Task<UserGetListOutput> UpdateAsync(int id, UserCreateUpdateInput input)
        {
            var userEntity = await _dbContext.Users.FirstOrDefaultAsync(x => x.Id == id);
            if (userEntity == null)
            {
                throw new ArgumentException("id is not found.");
            }
            userEntity.UserName = input.UserName;
            userEntity.Password = input.Password;
            _dbContext.Users.Update(userEntity);
            await _dbContext.SaveChangesAsync();

            return new UserGetListOutput
            {
                Id = userEntity.Id,
                UserName = userEntity.UserName,
                Password = userEntity.Password,
            };
        }

        public async Task<bool> ImportAsync(IAutoApiStreamContent input)
        {
            if (input.FileName.EndsWith(".xlsx") == false)
            {
                return false;
            }
            using var fileStream = input.GetStream();
            //TODO:导入处理excel
            return true;
        }
    }
}
